//
//  ActivityViewController.m
//  UI_APP_Project_Bean
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ActivityViewController.h"

#import "DetailOneViewController.h"
#import "NewActibityTableViewCell.h"
#import "ActivityModel.h"


#import "OwnerModel.h"



@interface ActivityViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)NSMutableArray *activityArray;

@property(nonatomic, retain)NSMutableArray *ownerArray;


@end

@implementation ActivityViewController

- (void)dealloc
{
    [_ownerArray release];
    [_activityArray release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self config];
    [self handleData];
    [self creatTableView];
}



-(void)config{
    self.navigationItem.title = @"活动";
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.37 green:0.6 blue:0.7 alpha:1];
    
    // 左右按钮图片颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.activityArray = [NSMutableArray array];
    self.ownerArray = [NSMutableArray array];
}

-(void)handleData{
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"activitylist" ofType:@".txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dicOfActivity = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *eventsArray = [dicOfActivity objectForKey:@"events"];
    
    for (NSDictionary *dic in eventsArray) {
        ActivityModel *activModel = [[ActivityModel alloc]init];
        
        [activModel setValuesForKeysWithDictionary:dic];
        
        [_activityArray addObject:activModel];
        
        
        for (NSString *key in dic) {
            if ([key isEqualToString:@"owner"]) {
                OwnerModel *own = [[OwnerModel alloc]init];
                NSDictionary *owner = [dic objectForKey: key];
                [own setValuesForKeysWithDictionary:owner];
                
                [self.ownerArray addObject:own];
            }
            
            
            
        }
        
        
        
        
        
    }
    
//    NSLog(@"%@", _activityArray);
    
}

-(void)creatTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    [self.view addSubview:tableView];
    [tableView release];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 200;
    
    [tableView registerClass:[NewActibityTableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.activityArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewActibityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.layer.cornerRadius = 5;
    cell.layer.masksToBounds = YES;
//    cell.backgroundColor = [UIColor colorWithRed:0.86 green:0.95 blue:0.9 alpha:1];
    
    
    cell.activityModel = [self.activityArray objectAtIndex:indexPath.row];
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 12.0;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailOneViewController *movicController = [[DetailOneViewController alloc]init];
    
    movicController.modelTwo = [self.activityArray objectAtIndex:indexPath.row];
    
    movicController.OwnerName = [self.ownerArray objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:movicController animated:YES];
    [movicController release];
    
    
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
